package com.benyanyi.picker;

import android.app.Activity;

import com.benyanyi.picker.bean.PickerBean;

import java.util.List;

/**
 * @author YanYi
 * @date 2020/6/20 17:13
 * @email ben@yanyi.red
 * @overview 仿京东地址选择器
 */
public class PickerHelper {

    private static PickerHelper instance;
    private PickerDialog pickerDialog;
    private Activity activity;
    private String title;
    private List<? extends PickerBean> oList;
    private PickerCallback pickerCallback;

    public static PickerHelper getInstance() {
        if (instance == null) {
            instance = new PickerHelper();
        }
        return instance;
    }

    private PickerHelper() {
    }

    public PickerHelper init(Activity activity) {
        init(activity, true);
        return this;
    }

    public PickerHelper init(Activity activity, boolean isCache) {
        if (this.activity == null) {
            this.activity = activity;
        }
        if (this.activity != activity) {
            this.activity = activity;
            this.pickerDialog = new PickerDialog(this.activity, isCache);
        } else if (this.pickerDialog == null) {
            this.pickerDialog = new PickerDialog(this.activity, isCache);
        }
        return this;
    }

    public PickerHelper setTitle(String title) {
        this.title = title;
        return this;
    }

    public <T extends PickerBean<?>> PickerHelper setData(List<T> list) {
        this.oList = list;
        return this;
    }

    public PickerHelper setPickerCallback(PickerCallback pickerCallback) {
        this.pickerCallback = pickerCallback;
        return this;
    }

    public void show() {
        if (this.pickerDialog != null) {
            if (!this.pickerDialog.isShowing()) {
                this.pickerDialog.dismiss();
            }
            this.pickerDialog.setPickerCallback(this.pickerCallback);
            this.pickerDialog.show(this.title, this.oList);
        }
    }

    public void dismiss() {
        if (this.pickerDialog != null && this.pickerDialog.isShowing()) {
            this.pickerDialog.dismiss();
        }
        this.pickerDialog = null;
    }


    public interface PickerCallback {
        void callback(List<String> msgList, List<Integer> indexList);
    }
}
